/**
 * Project: TacticaPescaditos
 * File: Wait.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TeamPlayer;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics.Vector2D;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: Wait
 */
public class Wait implements State<TeamPlayer> {
	
	private String name;

	/**
	 * 
	 */
	private Wait() {
		this.name = "Wait";
	}

	public static final State<TeamPlayer> instance = new Wait();

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#enter(java.lang.Object)
	 */
	public void enter(TeamPlayer o) {
		o.getSteering().setTarget(o.getHomePosition());
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#execute(java.lang.Object)
	 */
	public void execute(TeamPlayer player) {

		if (player.isAtTarget()) {
			player.getSteering().seekOff();
			player.setVelocity(new Vector2D());
		} else {
			player.getSteering().seekOn();

		}

		if (player.getTactica().isControlling() ) {
			if (	!player.isControlling()&&
					( player.inHotRegion() || player.isAheadAttacker())){
				player.getTactica().requestPass(player);
				return;
			}
		}

		if (player.isClosestToBall()){
			player.getStateMachine().changeState(ChaseBall.instance);
		} else if (player.isClosestToTrajectory()&&player.getTactica().getReceiver()==null){
			player.getStateMachine().changeState(InterceptPass.instance);
		}




	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#exit(java.lang.Object)
	 */
	public void exit(TeamPlayer player) {
		player.getSteering().seekOff();
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#onMessage(java.lang.Object, com.chidotronics.javacup.fsm.state.Telegram)
	 */
	public boolean onMessage(TeamPlayer o, Telegram telegram) {
		return false;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return this.name.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.name;
	}
}
